﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class Meeting
    {
        private Dictionary<int, Member> memberList = new Dictionary<int, Member>();
        private int coupleCount = 0;
        private bool canChange = false;
        public Meeting()
        {
            int testCase = GetConsole.GetInt();
            while(testCase--> 0)
            {
                //입력
                coupleCount = 0;
                memberList.Clear();
                memberList.Add(1, new Member(1, new int[5] { 6,7,8,9,10 }));

                for (int i = 2; i < 11; i++)
                {
                    int[] prefer = new int[5];
                    for (int j = 0; j < 5; j++)
                    {
                        prefer[j] = GetConsole.GetInt();
                    }
                    Member member = new Member(i, prefer);
                    memberList.Add(i, member);
                }
                //커플 정하기
                while(coupleCount < 5)
                {
                    for (int i = 10; i >5; i--)
                    {
                        if (memberList[i].CoupleMember == 0)
                        {
                            Propose(i);
                        }
                    }
                }
                //순위 바꾸기
                canChange = false;
                Member tehyun = memberList[1];
                if (tehyun.CoupleMember == 6 || tehyun.CoupleMember == 10) //이미 최상이거나 더이상 가망이 없을때
                { 
                    Console.WriteLine("NO");
                    continue;
                }
                for (int i = tehyun.CoupleMember + 1; i < 11; i++)
                {
                    if (memberList[i].GetMemberRank(1) < memberList[i].CoupleRank)
                    {
                        InterceptCouple(i, 1, tehyun.CoupleMember);
                    }
                }
                if (canChange)
                { Console.WriteLine("YES"); }
                else
                { Console.WriteLine("NO"); }
            }
        }
        private void InterceptCouple(int fromNumber, int targetNumber, int firstCouple)
        {
            if (fromNumber < firstCouple)
            {
                if (memberList[fromNumber].GetMemberRank(1) <= memberList[fromNumber].GetMemberRank(targetNumber))
                {
                    canChange = true;
                    return;
                }
            }
            if (memberList[targetNumber].CoupleMember == 0)
            {
                memberList[targetNumber].CoupleMember = targetNumber;
                return;
            }
            int beforeCouple = 0;
            if (targetNumber == 1 || memberList[targetNumber].GetMemberRank(fromNumber) < memberList[targetNumber].CoupleRank)
            {
                beforeCouple = memberList[targetNumber].CoupleMember;
                //커플 뺏기
                memberList[fromNumber].CoupleMember = targetNumber;
                memberList[targetNumber].CoupleMember = fromNumber;
                //퇴짜 처리
                
                memberList[beforeCouple].CoupleMember = 0;
                memberList[beforeCouple].AddProposeCount();
                InterceptCouple(beforeCouple, memberList[beforeCouple].PreferMember, firstCouple);
            }
            else
            {
                memberList[fromNumber].AddProposeCount();
                InterceptCouple(fromNumber, memberList[fromNumber].PreferMember, firstCouple);
            }
        }
        private void Propose(int fromNumber)
        {
            int toNumber = memberList[fromNumber].PreferMember;
            Member toMember = memberList[toNumber];
            Member fromMember = memberList[fromNumber];
            if (toMember.CoupleMember > 0)
            {
                if (toMember.GetMemberRank(fromNumber) < toMember.CoupleRank)
                {
                    memberList[toMember.CoupleMember].CoupleMember = 0;
                    fromMember.CoupleMember = toNumber;
                    toMember.CoupleMember = fromNumber;
                }
            }
            else
            {
                fromMember.CoupleMember = toNumber;
                toMember.CoupleMember = fromNumber;
                coupleCount++;
            }
            fromMember.AddProposeCount();
        }

        class Member
        {
            private int number = 0;
            private int[] memberInfo = new int[11];

            private int[] preferList = null;
            public int PreferMember { get { return preferList[proposeCount]; } }

            private int coupleMember = 0;
            public int CoupleMember { get { return coupleMember; } set { coupleMember = value; } }
            public int CoupleRank { get { return memberInfo[coupleMember]; } }

            private int proposeCount = 0;

            public int GetMemberRank(int member)
            {
                return memberInfo[member];
            }
            public void AddProposeCount()
            {
                proposeCount++;
            }
            public Member(int number, int[] prefer)
            {
                this.number = number;
                preferList = prefer;
                for (int i = 0; i < prefer.Length; i++)
                {
                    memberInfo[prefer[i]] = i + 1;
                }
            }
        }
    }

}
